/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wf.units;

import bc.dsl.SwingDSL;
import bc.utils.SHA1Calc;
import com.lowagie.text.pdf.codec.Base64.OutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import wf.model.User;
import wf.ui.Login;
import wf.ui.WFUserInterface;

/**
 *
 * @author bennyl
 */
public enum ApplicationUnit {

    UNIT;
    private static PrintWriter pwe = null;
    private static PrintWriter pwo = null;
    private User registeredUser = null;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        DatabaseUnit.UNIT.connect();
        startPLCDeamon();
        defineLogs();

        //        System.err.println("DEAMON Is NOT WORKiNG!!!!");
        UNIT.restartUI();
        
    }

    public void restartUI() {
        WFUserInterface ui = new WFUserInterface();
        ui.startUI();
    }

    private static void startPLCDeamon() {
        Thread t = new Thread(PLCComUnit.UNIT);
        t.start();
    }

    public User getRegisteredUser() {
        return registeredUser;
    }

    public User invokeUserRegistration() {
        final Login login = new Login();
        while (true) {
            wf.ui.windows.Dialog.show(login, 400, 150);

            if (login.getUser() != null) {
                User temp = login.getUser();
                registeredUser = DatabaseUnit.UNIT.selectUserByName(temp.getName());
                if (registeredUser == null || !registeredUser.getPassHash().equals(SHA1Calc.calc(login.getPassword()))) {
                    SwingDSL.errbox("Wrong Cradentials", "Bad user name or password");
                } else {
                    break;
                }
            } else {
                invokeApplicationClose();
            }
        }

        return registeredUser;
    }

    public void invokeApplicationClose() {
        if (SwingDSL.ynqbox("Exiting Application", "Are you sure you want to exit Waterfall?")) {
            System.exit(0);
        }
    }

    private static void defineLogs() {
        try {
            File errLog = new File("err.log");
            pwe = new PrintWriter(errLog);
            File outLog = new File("out.log");
            pwo = new PrintWriter(outLog);
            final PrintStream oldErr = System.err;
            final PrintStream oldOut = System.out;

            System.setErr(new PrintStream(new java.io.OutputStream() {

                @Override
                public void write(int b) throws IOException {
                    pwe.write(b);
                    oldErr.write(b);
                }
            }));

            System.setOut(new PrintStream(new java.io.OutputStream() {

                @Override
                public void write(int b) throws IOException {
                    pwo.write(b);
                    oldOut.write(b);
                }
            }));

        } catch (IOException ex) {
            Logger.getLogger(ApplicationUnit.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            Runtime.getRuntime().addShutdownHook(new Thread(){

                @Override
                public void run() {
                    pwo.close();
                    pwe.close();
                }
                
            });
        }
    }
}
